﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HermesWinForm
{
    public partial class DesigatedCarrier : Form
    {

        bool selectCountry = false;
        bool selectCarrier = false;

        public DesigatedCarrier()
        {
            InitializeComponent();
        }

        private void DesigatedCarrier_Load(object sender, EventArgs e)
        {
            string[] siteNames = SiteInfo.GetSiteName();
            for (int i = 0; i < siteNames.Length; ++i)
            {
                cbSites.Items.Insert(i, siteNames[i]);
            }

            string[] carrierNames = CarrierInfo.GetCarrierNames();
            for (int i = 0; i < carrierNames.Length; ++i)
            {
                cbCarriers.Items.Insert(i, carrierNames[i]);
            }


            btnFindSiteOrders.Enabled = false;
            panel1.Visible = false;

            btnAssign.Enabled = false;
        }

        private void cbSites_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbSites.SelectedItem.ToString() != "")
            {
                btnFindSiteOrders.Enabled = true;
            }
            else
            {
                btnFindSiteOrders.Enabled = false;
            }
        }

        private void btnFindSiteOrders_Click(object sender, EventArgs e)
        {
            string siteName = cbSites.SelectedItem.ToString();

            OrdersServiceReference.SalesOrderSoapClient orderClient = new OrdersServiceReference.SalesOrderSoapClient();
            OrdersServiceReference.UnCarrierOrdersInfo[] infos = orderClient.GetUnCarrierOrders(SiteInfo.GetSiteID(siteName));

            for (int i = 0; i < infos.Length; ++i)
            {
                infos[i].DisplayName = infos[i].Country + "[" + infos[i].Count.ToString() + "]";
            }

            chklistCountry.DataSource = infos;
            chklistCountry.DisplayMember = "DisplayName";
            chklistCountry.ValueMember = "Country";

            panel1.Visible = true;
            //btnAssign.Enabled = false;

            for (int i = 0; i < chklistCountry.Items.Count; i++)
            {
                if (chklistCountry.GetItemChecked(i) == true)
                {
                    //MessageBox.Show(chklistCountry.GetItemText(chklistCountry.Items[i]));//获取DisplayMember
                    selectCountry = true;
                    break;
                }
                else
                {
                    selectCountry = false;
                }
            }
            if (chklistCountry.Items.Count == 0)
            {
                selectCountry = false;
            }

            if (cbCarriers.SelectedItem !=null && cbCarriers.SelectedItem.ToString() != "")
            {
                selectCarrier = true;
            }
            else
            {
                selectCarrier = false;
            }
            if (selectCarrier && selectCountry)
            {
                btnAssign.Enabled = true;
            }
            else
            {
                btnAssign.Enabled = false;
            }

        }

        

        private void chklistCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < chklistCountry.Items.Count; i++)
            {
                if (chklistCountry.GetItemChecked(i) == true)
                {
                    //MessageBox.Show(chklistCountry.GetItemText(chklistCountry.Items[i]));//获取DisplayMember
                    selectCountry = true;
                    break;
                }
                else
                {
                    selectCountry = false;
                }
            }

            if (selectCarrier && selectCountry)
            {
                btnAssign.Enabled = true;
            }
            else
            {
                btnAssign.Enabled = false;
            }


        }

        private void cbCarriers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbCarriers.SelectedItem != null && cbCarriers.SelectedItem.ToString() != "")
            {
                selectCarrier = true;
            }
            else
            {
                selectCarrier = false;
            }

            if (selectCarrier && selectCountry)
            {
                btnAssign.Enabled = true;
            }
            else
            {
                btnAssign.Enabled = false;
            }
        }

        private void btnAssign_Click(object sender, EventArgs e)
        {
            string countries = "";

            for (int i = 0; i < chklistCountry.Items.Count; i++)
            {
                if (chklistCountry.GetItemChecked(i) == true)
                {
                    OrdersServiceReference.UnCarrierOrdersInfo info = chklistCountry.Items[i] as OrdersServiceReference.UnCarrierOrdersInfo;
                    countries += info.Country + ";";
                }
            }
            

            int carrierID = CarrierInfo.GetCarrierID(cbCarriers.SelectedItem.ToString());
            int siteID = SiteInfo.GetSiteID(cbSites.SelectedItem.ToString());

            if (countries.Length > 0 && carrierID > 0 && siteID > 0)
            {
                countries = countries.Substring(0, countries.Length - 1);

                OrdersServiceReference.SalesOrderSoapClient orderClient = new OrdersServiceReference.SalesOrderSoapClient();
                int count = orderClient.AssignCarrier(carrierID, siteID, countries);
                MessageBox.Show(count.ToString() + "个订单将采用" + cbCarriers.SelectedItem.ToString() + " 发运");
                btnFindSiteOrders_Click(sender, e);
            }

        }
    }
}
